"""
执行操作
1. 冒烟测试
"""
from testcases.testuser import TestUser
from testcases.testtopic import TestTopic

import unittest
import yaml

def create_smoketest():
    """
    冒烟测试套件
    1. 登录测试
    2. 发帖测试
    :return:
    """
    # 实例化测试套件类
    ts = unittest.TestSuite()
    # 添加TestUser中 test_login 测试用例
    ts.addTest(TestUser('test_login'))
    ts.addTest(TestTopic('test_new_topic'))
    return ts



def create_CI():
    ts = unittest.TestSuite()
    # Testcase加载器
    loader = unittest.TestLoader()
    # 加载testcases目录下所有的test*.py 文件的 TestCase
    alltests = loader.discover(start_dir='testcases',pattern='test*.py')
    # 将所有的测试用例存放到测试套件中
    ts.addTests(alltests)
    return ts

import os
task = os.getenv('alltask')

def create_jenkins_suite():
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    yamldata = yaml.safe_load(open('./config.yaml', encoding='utf8'))
    if task:
        testcases = yamldata.get(task)
    else:
        testcases = yamldata.get('话题')
    tests = loader.loadTestsFromNames(testcases)
    suite.addTests(tests)
    return suite

if __name__ == '__main__':
    """
    测试执行器
    """
    # 创建测试执行器
    import xmlrunner

    runner = xmlrunner.XMLTestRunner(output='reports')
    # runner = unittest.TextTestRunner(verbosity=2)
    # 运行测试套件
    runner.run(create_jenkins_suite())